﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;

//DT
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Data.SqlClient;


namespace parking
{
    public class AccountImpl
    {
        static dbDataContext db = new dbDataContext();

        //DT
        // Static variables for AccountType definition
        internal const int ACCOUNT_VISITOR = 0;
        internal const int ACCOUNT_STUDENT = 1;
        internal const int ACCOUNT_STAFF = 2;
        internal const int ACCOUNT_ADMIN = 3;


        internal static int createAccount(string email, string password, int accountType, string firstName, string lastName, string address, string city, string state, string zip)
        {
            try
            {
                user myUser = new user();
                myUser.email = email;
                myUser.password = password;
                myUser.accountType = accountType;
                myUser.firstname = firstName;
                myUser.lastname = lastName;
                myUser.address = address;
                myUser.city = city;
                myUser.state = state;
                myUser.zip = zip;


                db.users.InsertOnSubmit(myUser);
                db.SubmitChanges();

                return myUser.id;
            }
            catch(Exception){
                return -1;
            }
        }

        internal static int getAccountTypeByEmail(string email)
        {
            user myUser = (from u in db.users
                           where u.email == email
                           select u).SingleOrDefault();

            if (myUser != null)
                return myUser.accountType;
            else
                return 0;
        }

        internal static string getAccountDetails(int userId)
        {
            //get the user from DB
            user myUser = (from u in db.users
                          where u.id == userId
                          select u).SingleOrDefault();

            //create the root node of the xml
            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);

            XmlNode accountNode = doc.CreateElement("account");
            doc.AppendChild(accountNode);

            //add id
            XmlAttribute attr = doc.CreateAttribute("id");
            attr.Value = myUser.id.ToString();
            accountNode.Attributes.Append(attr);

            //add the rest of account details as members
            XmlNode propNode = doc.CreateElement("email");
            propNode.AppendChild(doc.CreateTextNode(myUser.email));
            accountNode.AppendChild(propNode);

            propNode = doc.CreateElement("accounttype");
            propNode.AppendChild(doc.CreateTextNode(myUser.accountType.ToString()));
            accountNode.AppendChild(propNode);

            propNode = doc.CreateElement("firstname");
            propNode.AppendChild(doc.CreateTextNode(myUser.firstname));
            accountNode.AppendChild(propNode);

            propNode = doc.CreateElement("lastname");
            propNode.AppendChild(doc.CreateTextNode(myUser.lastname));
            accountNode.AppendChild(propNode);

            propNode = doc.CreateElement("address");
            propNode.AppendChild(doc.CreateTextNode(myUser.address));
            accountNode.AppendChild(propNode);

            propNode = doc.CreateElement("city");
            propNode.AppendChild(doc.CreateTextNode(myUser.city));
            accountNode.AppendChild(propNode);

            propNode = doc.CreateElement("state");
            propNode.AppendChild(doc.CreateTextNode(myUser.state));
            accountNode.AppendChild(propNode);

            propNode = doc.CreateElement("zip");
            propNode.AppendChild(doc.CreateTextNode(myUser.zip));
            accountNode.AppendChild(propNode);

            //convert the XML into string
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

            doc.WriteTo(xmlTextWriter);
            xmlTextWriter.Flush();

            return stringWriter.GetStringBuilder().ToString();

        }

        //DT
        internal static bool login(string email, string password)
        {
            var theUser = (from result in db.users
                       where result.email == email && result.password == password
                       select result).SingleOrDefault();

            /*
            try
            {
                // update the database by adding YES to the "islogin" column
                theUser.islogin = "yes";
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                //TODO: Add some exceptions here...
            }
            */

            return theUser != null ? true : false;
            
        }

        //DT
        internal static bool logout(string email)
        {
            var theUser = (from result in db.users
                            where result.email == email
                            select result).SingleOrDefault();
            /*
            try
            {
                // Update the database by setting to NO for the "islogin" column
                theUser.islogin = "no";
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
               //TODO: Add some exceptions here...
            }
            */
            return theUser != null ? true: false;
         }

    }//end class
}//end namespace